//
//  FaceView.m
//  WXWeibo
//
//  Created by apple on 16/5/20.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "FaceView.h"
#define kFaceItemSize 45
#define kFaceImage 30
#define kLinesPerPage 4

@interface FaceView ()

@property (nonatomic, strong) UIImageView *selectImg;

@property (nonatomic, copy) NSString *selectFaceName;
@end

@implementation FaceView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self _createData];

        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (UIImageView *)selectImg {
    
    if (_selectImg == nil) {
        
        _selectImg = [[UIImageView alloc] initWithFrame:CGRectZero];
        _selectImg.image = [UIImage imageNamed:@"emoticon_keyboard_magnifier"];
        [_selectImg sizeToFit];
        [self addSubview:_selectImg];
        
        UIImageView *faceImg = [[UIImageView alloc] initWithFrame:CGRectMake((_selectImg.width - kFaceImage) / 2, 15, kFaceImage, kFaceImage)];
        
        faceImg.tag = 1000;
        
        [_selectImg addSubview:faceImg];
    }
    
    return _selectImg;
}

- (void)_createData {
    
    _faceArr2D = [NSMutableArray array];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"];
    NSArray *emotions = [NSArray arrayWithContentsOfFile:filePath];
    
    NSMutableArray *faceArr1D = nil;
    
    NSInteger columnsPerline = kScreenWidth / kFaceItemSize;
    NSUInteger count = kLinesPerPage * columnsPerline;
    
    for (NSInteger i = 0; i < emotions.count; i++) {
        
        if (faceArr1D == nil || faceArr1D.count == count) {
            
            faceArr1D = [NSMutableArray arrayWithCapacity:count];
            
            [_faceArr2D addObject:faceArr1D];
        }
        
        
        [faceArr1D addObject:emotions[i]];
    }
    
    CGRect frame = self.frame;
    
    frame.size.width = _faceArr2D.count * kScreenWidth;
    frame.size.height = kFaceItemSize * kLinesPerPage;
    
    self.frame = frame;
    
}
- (void)drawRect:(CGRect)rect {
    
    NSInteger row = 0;
    NSInteger column = 0;
    
    NSInteger columnPerline = kScreenWidth / kFaceItemSize;
    
    for (NSInteger i = 0; i < _faceArr2D.count; i++) {
        
        NSArray *faceArr1D = _faceArr2D[i];
        
        for (NSInteger j = 0; j < faceArr1D.count; j++) {
            
            NSDictionary *item = faceArr1D[j];
            NSString *imgName = item[@"png"];
            UIImage *image = [UIImage imageNamed:imgName];
            
            CGFloat x = column * kFaceItemSize + (kFaceItemSize - kFaceImage) / 2 + i *kScreenWidth;
            CGFloat y = row * kFaceItemSize + (kFaceItemSize - kFaceImage) / 2;
            
            [image drawInRect:CGRectMake(x, y, kFaceImage, kFaceImage)];
            
            column++;
            if (column % columnPerline == 0) {
                column = 0;
                row++;
            }
            if (row == kLinesPerPage) {
                row = 0;
            }
        }
    }
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    self.selectImg.hidden = NO;
    
    
    
    UITouch *touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scroll = (UIScrollView *)self.superview;
        
        scroll.scrollEnabled = NO;
    }
    
    [self touchFace:point];
}

- (void)touchFace:(CGPoint)point {
   
    
    NSInteger page = point.x / kScreenWidth;
    
    if (page >= _faceArr2D.count || page < 0) {
        
        return;
    }
    NSInteger column = (point.x - ((kFaceItemSize - kFaceImage) / 2 + page * kScreenWidth))/ kFaceItemSize;
    NSInteger row = (point.y - (kFaceItemSize - kFaceImage) / 2) / kFaceItemSize;
    
    NSInteger columnPerline = kScreenWidth / kFaceItemSize;
//    if (column > columnPerline - 1) {
//        column = columnPerline  -1;
//    }
//    if (column < 0) {
//        column = 0;
//    }
    column = MIN(columnPerline - 1, MAX(0, column));
    
//    if (row > kLinesPerPage - 1) {
//        row = kLinesPerPage - 1;
//    }
//    
//    if (row < 0) {
//        row = 0;
//    }
    row = MIN(kLinesPerPage - 1, MAX(0, row));
    
    NSInteger index = row * columnPerline + column;
    
    NSArray *faceArr1D = _faceArr2D[page];
    if (index >= faceArr1D.count || index < 0) {
        self.selectFaceName = nil;
        return;
    }
    
    NSDictionary *faceInfo = faceArr1D[index];
    NSString *imageName = faceInfo[@"png"];
    _selectFaceName = faceInfo[@"chs"];
    
    [self selectImg];

    UIImageView *faceImageView = (UIImageView *)[_selectImg viewWithTag:1000];
    faceImageView.image = [UIImage imageNamed:imageName];
    
    CGFloat x = column * kFaceItemSize + kFaceItemSize / 2 + page * kScreenWidth;
    CGFloat y = row * kFaceItemSize + kFaceItemSize / 2;
    
    self.selectImg.center = CGPointMake(x, 0);
    self.selectImg.bottom = y;
    
    
    
//    if (_block != nil) {
//        
//        _block(faceName);
//    }
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    
    CGPoint point = [touch locationInView:self];
    
    [self touchFace:point];
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    self.selectImg.hidden = YES;
    
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scroll = (UIScrollView *)self.superview;
        
        scroll.scrollEnabled = YES;
    }
    
    if ([self.delegate respondsToSelector:@selector(faceViewDidSelectFace:)]) {
        [self.delegate faceViewDidSelectFace:self.selectFaceName];
    }
}

@end
